package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.admin.common.ElementConst;
import com.soft.dao.ChannelDao;
import com.soft.dao.TeachDao;
import com.soft.front.dto.LocationDTO;
import com.soft.front.dto.TeachDTO;
import com.soft.model.Channel;
import com.soft.model.Teach;
import com.soft.support.Pager;
import com.soft.util.DateUtil;

@SuppressWarnings("unchecked")
@Component
public class TeachDaoImpl extends BaseDaoImpl implements TeachDao {
	private ChannelDao channelDao;
	
	@Autowired
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

	@CacheEvict(value = "TEACH_INFO_CACHE", allEntries = true)
	public void addOrEdit(Teach teach) {
		this.saveOrUpdate(teach);
	}

	@CacheEvict(value = "TEACH_INFO_CACHE", allEntries = true)
	public void remove(Teach teach) {
		this.delete(teach);
	}

//	@Cacheable(value = "TEACH_INFO_CACHE", key = "#id")
	public Teach get(Integer id) {
		return (Teach)this.get(Teach.class, id);
	}

	@Cacheable(value = "TEACH_INFO_CACHE", key = "#index + #page + #pageSize + 'getLst'")
	public Map<String, Object> getLst(String index, Integer page, Integer pageSize) {
		//返回结果
		Map<String, Object> resMap = new HashMap<String, Object>();
		//分页
		if (page == null) page = 1;
		Integer startIndex = (page - 1) * pageSize;
//		Integer endIndex = startIndex + pageSize;
		//获取栏目信息
		Channel indexChannel = null;
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.eq("state", 1));
		Channel chan = this.channelDao.get(ElementConst.JKYCONST);
		if ("".equals(index)) {
			indexChannel = chan;
		} else {
			indexChannel = this.channelDao.get(index);
			values.add(Restrictions.eq("channel.id", indexChannel.getId()));
		}
		//获取相对应信息.
		Pager teachLst = this.findForPager(Teach.class, new Order[]{Order.desc("level"), Order.desc("releaseDate")}, 
				startIndex, pageSize, values);
		List<TeachDTO> teachDTO = new ArrayList<TeachDTO>();
		for (Teach teach : (List<Teach>)teachLst.getDatas()) {
			TeachDTO dto = new TeachDTO();
			BeanUtils.copyProperties(teach, dto);
			dto.setDate(DateUtil.format(teach.getReleaseDate(), "yyyy-MM-dd"));
			dto.setUrl(ElementConst.JKYCONST + "/" + teach.getChannel().getNamespace() + "/" + teach.getId() + ".jhtml");
			dto.setChannelName(teach.getChannel().getName());
			teachDTO.add(dto);
		}
		resMap.put("teachs", teachDTO);
		resMap.put("total", teachLst.getTotal());
		resMap.put("pageIndex", page);
		int pages = 0;
		if (teachLst.getTotal() !=0 && teachLst.getTotal() % ElementConst.PAGENUM == 0) {
			pages = teachLst.getTotal() / ElementConst.PAGENUM;
		} else {
			pages = teachLst.getTotal() / ElementConst.PAGENUM + 1;
		}
		resMap.put("pages", pages);
		resMap.put("url", ElementConst.JKYCONST + "/index.jhtml");
		resMap.put("title", chan.getName());
		//位置定位
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.JKYCONST + "/index.jhtml");
		locationDTO.add(doo);
		if (!"".equals(index)) {
			LocationDTO doo1 = new LocationDTO();
			doo1.setName(indexChannel.getName());
			doo1.setUrl(ElementConst.JKYCONST + "/" + index + "/index.jhtml");
			locationDTO.add(doo1);
			resMap.put("url", ElementConst.JKYCONST + "/" + index + "/index.jhtml");
			resMap.put("title", indexChannel.getName());
		}
		resMap.put("locations", locationDTO);
		return resMap;
	}

	@Cacheable(value = "TEACH_INFO_CACHE", key = "#id + #index + 'get'")
	public Map<String, Object> get(String index, Integer id) {
		//返回结果
		Map<String, Object> resMap = new HashMap<String, Object>();
		Teach teach = (Teach)this.get(id);
		resMap.put("title", teach.getTitle());
		resMap.put("txt", teach.getTxt());
		resMap.put("releaseDate", DateUtil.format(teach.getReleaseDate()));
		//位置定位
		Channel channel = teach.getChannel();
		Channel chan = this.channelDao.get(ElementConst.JKYCONST);
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.JKYCONST + "/index.jhtml");
		locationDTO.add(doo);
		LocationDTO doo1 = new LocationDTO();
		doo1.setName(channel.getName());
		doo1.setUrl(ElementConst.JKYCONST + "/" + channel.getNamespace() + "/index.jhtml");
		locationDTO.add(doo1);
		resMap.put("locations", locationDTO);
		LocationDTO doo2 = new LocationDTO();
		doo2.setName("正文");
		doo2.setUrl(ElementConst.JKYCONST + "/" + channel.getNamespace() + "/" + id + ".jhtml");
		locationDTO.add(doo2);
		return resMap;
	}
	
}
